from alibabacloud_bailian20231229.client import Client as bailian20231229Client
from alibabacloud_tea_openapi.client import Client as OpenApiClient
from alibabacloud_credentials.client import Client as CredentialClient
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
import hashlib
from app.database.db_setup import Base 
from sqlalchemy import Column, Integer, String, ForeignKey,SmallInteger
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from alibabacloud_openapi_util.client import Client as OpenApiUtilClient
import json
class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(access_key_id, access_key_secret) -> bailian20231229Client:
        """
        使用 AK 初始化账号 Client
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            access_key_id=access_key_id,
            access_key_secret=access_key_secret
        )
        # Endpoint 请参考 https://api.aliyun.com/product/bailian
        config.endpoint = f'bailian.cn-beijing.aliyuncs.com'
        return bailian20231229Client(config)

    @staticmethod
    def create_api_info(
        workspace_id: str,
    ) -> open_api_models.Params:
        """
        API 相关
        @param path: string Path parameters
        @return: OpenApi.Params
        """
        params = open_api_models.Params(
            # 接口名称,
            action='CreateIndex',
            # 接口版本,
            version='2023-12-29',
            # 接口协议,
            protocol='HTTPS',
            # 接口 HTTP 方法,
            method='POST',
            auth_type='AK',
            style='ROA',
            # 接口 PATH,
            pathname=f'/{workspace_id}/index/create',
            # 接口请求体内容格式,
            req_body_type='json',
            # 接口响应体内容格式,
            body_type='json'
        )
        return params

    @staticmethod
    def call_api(
        access_key_id,
        access_key_secret,
        workspace_id,
        category_id,
        name,
        StructureType,
        SourceType,
        SinkType
    ):
        client = Sample.create_client(access_key_id, access_key_secret)
        params = Sample.create_api_info(workspace_id)
        # query params
        queries = {}
        queries['Name'] = name
        queries['StructureType'] = StructureType
        queries['SourceType'] = SourceType
        # 初始化空列表
        cat_list = []
        # 动态添加元素
        cat_list.append(category_id)
        # 转换为 JSON 字符串
        cat_str = json.dumps(cat_list, ensure_ascii=False)
        queries['CategoryIds'] = cat_str
        queries['SinkType'] = SinkType
        # runtime options
        runtime = util_models.RuntimeOptions()
        request = open_api_models.OpenApiRequest(
            query=OpenApiUtilClient.query(queries)
        )
        result = client.call_api(params, request, runtime)
        return result
class AddDocumentsJob:
    def __init__(self):
        pass

    @staticmethod
    def create_client(access_key_id, access_key_secret) -> bailian20231229Client:
        """
        使用 AK 初始化账号 Client
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            access_key_id=access_key_id,
            access_key_secret=access_key_secret
        )
        # Endpoint 请参考 https://api.aliyun.com/product/bailian
        config.endpoint = f'bailian.cn-beijing.aliyuncs.com'
        return bailian20231229Client(config)

    @staticmethod
    def create_api_info(
        workspace_id: str,
    ) -> open_api_models.Params:
        """
        API 相关
        @param path: string Path parameters
        @return: OpenApi.Params
        """
        params = open_api_models.Params(
            # 接口名称,
            action='SubmitIndexAddDocumentsJob',
            # 接口版本,
            version='2023-12-29',
            # 接口协议,
            protocol='HTTPS',
            # 接口 HTTP 方法,
            method='POST',
            auth_type='AK',
            style='ROA',
            # 接口 PATH,
            pathname=f'/{workspace_id}/index/add_documents_to_index',
            # 接口请求体内容格式,
            req_body_type='json',
            # 接口响应体内容格式,
            body_type='json'
        )
        return params

    @staticmethod
    def call_api(
        access_key_id,
        access_key_secret,
        workspace_id,
        IndexId,
        category_id
    ):
        client = AddDocumentsJob.create_client(access_key_id, access_key_secret)
        params = AddDocumentsJob.create_api_info(workspace_id)
        # query params
        queries = {}
        queries['IndexId'] =IndexId
        queries['SourceType'] = 'DATA_CENTER_CATEGORY'
        # 初始化空列表
        cat_list = []
        # 动态添加元素
        cat_list.append(category_id)
        # 转换为 JSON 字符串
        cat_str = json.dumps(cat_list, ensure_ascii=False)
        queries['CategoryIds'] = cat_str
        # runtime options
        runtime = util_models.RuntimeOptions()
        request = open_api_models.OpenApiRequest(
            query=OpenApiUtilClient.query(queries)
        )
        result = client.call_api(params, request, runtime)
        return result
class AiIndex(Base):
    __tablename__ = 'rk_ai_index'
    id = Column(Integer, primary_key=True, autoincrement=True, comment='ID')
    CategoryId = Column(String(200), nullable=False, default='', comment='CategoryId')
    script_id = Column(Integer, nullable=False, comment='话术id')
    name = Column(String(200), nullable=False, default='', comment='话术名称')
    index_id = Column(String(50), nullable=False, default='', comment='知识库的id')
    add_time = Column(Integer, nullable=False, default=0, comment='添加记录的时间')
    